<?php

namespace App\Lib\Jos;

use GuzzleHttp\Client;

class JosClient
{
    public $appKey = "107519AB6EC098501F4836D2408B44E4";

    public $appSecret = "935990b99bbb4cc18c25dc630a83fc65";

    public $appKeyV2 = '61bea65e1fa61c15eaab2358db919264';

    public $appSecretV2 = '72a8b288b9424666a83d7cb1b1d3cbe9';

    public $baseParam;

    public $httpClient;

    public function __construct(){
        $this->baseParam = [
            "360buy_param_json" => "",
        ];
        $this->httpClient= new Client();
    }

    public function request($method,$params){

        log_daily('jos client')->info("request param",['method' => $method,'params'=>$params]);;

        $params = json_encode($params);
        $date = date("Y-m-d H:i:s");

        $sign = strtoupper(md5("{$this->appSecret}360buy_param_json{$params}app_key{$this->appKey}method{$method}timestamp{$date}v2.0{$this->appSecret}"));

        $url = "https://api.jd.com/routerjson?";
        $url .= "360buy_param_json={$params}";
        $url .= "&app_key={$this->appKey}";
        $url .= "&method={$method}";
        $url .= "&timestamp={$date}";
        $url .= "&v=2.0";
        $url .= "&sign={$sign}";


        $res =  $this->httpClient->request('get',$url,['verify'=>false]);

        log_daily('jos client')->info("result:{$res->getBody()}");

        $res = json_decode($res->getBody(),true);
        return $res;

    }

    public function requestV2($method,$params){

        log_daily('jos client')->info("request param",['method' => $method,'params'=>$params]);;

        $params = json_encode($params);
        $date = date("Y-m-d H:i:s");

        $sign = strtoupper(md5("{$this->appSecretV2}360buy_param_json{$params}app_key{$this->appKeyV2}method{$method}timestamp{$date}v2.0{$this->appSecretV2}"));

        $url = "https://api.jd.com/routerjson?";
        $url .= "360buy_param_json={$params}";
        $url .= "&app_key={$this->appKeyV2}";
        $url .= "&method={$method}";
        $url .= "&timestamp={$date}";
        $url .= "&v=2.0";
        $url .= "&sign={$sign}";

        $res =  $this->httpClient->request('get',$url,['verify'=>false]);

        log_daily('jos client')->info("result:{$res->getBody()}");

        $res = json_decode($res->getBody(),true);
        return $res;

    }

}
